package com.smi.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

/**
 * Created by yesiming on 2017/6/12.
 */
public class SqlGenerate2 implements Generate {

    private static final String SPACE = " ";
    private static final String NOSPACE = "";
    private static final String COMMA = ",";
    private static final String APOSTROPHE = "`";

    
    public String genInsert(String dbName, String tableName, boolean isUUID, boolean isUUIDAuto) {

        String paraName = GenUtil.replaceUnderLineWithOutFirstLetter(tableName);



        StringBuffer sb = new StringBuffer("");
        if(!isUUIDAuto) {
           sb.append("@Insert(");
        } else {
            sb.append("@SelectKey(keyProperty = \"");
            sb.append(paraName);
            sb.append(".id\"");
            sb.append(",resultType = String.class, before = true,");
            sb.append("\n        statement = \"select replace(uuid(), '-', '')\")");
            sb.append("\r\n@Options(keyProperty = \"");
            sb.append(paraName);
            sb.append(".id\"");
            sb.append(", useGeneratedKeys = true)");
            sb.append("\r\n@Insert(");
        }
        sb.append("\"insert into ");
        sb.append(tableName);
        sb.append("(");

        StringBuffer sb2 = new StringBuffer(" values (");

        final ResultSet rs = getResult(tableName, dbName);
        try {
            while (rs.next()) {
                String para = rs.getString(1);
                String type = rs.getString(2);

                if(!isUUID && "id".equals(para)) {
                    continue;
                }
                if("timestamp".equals(type) || "datetime".equalsIgnoreCase(type)) {
                    continue;
                }
                sb.append("\"");
                sb.append(para);
                sb.append(", \" + ");
                sb.append("\r\n");

                sb2.append("\"");
                if("id".equals(para)) {
                    if(!isUUIDAuto) {
                        sb2.append("REPLACE(UUID(), '-', '')");
                    } else {
                        sb2.append("#{");
                        sb2.append(paraName);
                        sb2.append(".");
                        sb2.append(para);
                        sb2.append("}");
                    }

                } else {
                    sb2.append("#{");
                    sb2.append(paraName);
                    sb2.append(".");
                    sb2.append(para);
                    sb2.append("}");
                }

                sb2.append(", \" + ");
                sb2.append("\r\n");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        sb.append("\")");
        sb2.append(")");

        sb.append(sb2);

        return sb.toString().
                replace(", )", ")").
                replace("(\"", "(").
                replace("@Insert(", "@Insert(\"").
                replace(", \" + \r\n\")", " \" + \r\n\")").
                replace("}, \" + \r\n" +
                        ")", "}) \"  \r\n" +
                ")");

    }


    public static void main(String [] args) {
        String str = "\"#{loans.loan_progress}, \" + \n" +
                ")";
        String str1 = str.replace("}, \" + \n" +
                ")", "}) \"  \n" +
                ")");
        System.out.println(str + "|||" + str1);

    }


    public String genInsertWithinLog(String dbName, String tableName, boolean isUUID, String sqlLog) {
        sqlLog = sqlLog.replace(SPACE, NOSPACE).replace(APOSTROPHE, NOSPACE);
        String[] attrs = sqlLog.split(COMMA);

        List<String> attrList = Arrays.asList(attrs);

        String paraName = GenUtil.replaceUnderLineWithOutFirstLetter(tableName);

        StringBuffer sb = new StringBuffer("@Insert(");
        sb.append("\"insert into ");
        sb.append(tableName);
        sb.append("(");

        StringBuffer sb2 = new StringBuffer(" values (");

        final ResultSet rs = getResult(tableName, dbName);
        try {
            while (rs.next()) {
                String para = rs.getString(1);
                if(!isUUID && "id".equals(para)) {
                    continue;
                }
                sb.append("\"");
                sb.append(para);
                sb.append(", \" + ");
                sb.append("\r\n");

                sb2.append("\"");
                if("id".equals(para)) {
                    sb2.append("REPLACE(UUID(), '-', '')");
                } else {
                    sb2.append("#{");
                    sb2.append(paraName);
                    sb2.append(".");
                    sb2.append(para);
                    sb2.append("}");
                }

                sb2.append(", \" + ");
                sb2.append("\r\n");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        sb.append("\")");
        sb2.append(")");

        sb.append(sb2);

        return sb.toString().
                replace(", )", ")").
                replace("(\"", "(").
                replace("@Insert(", "@Insert(\"").
                replace(", \" + \r\n\")", " \" + \r\n\")").
                replace("}, \" + \r\n" +
                        ")", "}) \"  \r\n" +
                        ")")
        ;

    }

    public String genUpdate(String dbName, String tableName) {

        String paraName = GenUtil.replaceUnderLineWithOutFirstLetter(tableName);

        StringBuffer sb = new StringBuffer("@Update(\"<script>\" +");
        sb.append("\r\n");
        sb.append("\"update ");
        sb.append(tableName);
        sb.append(" set \" + ");
        sb.append("\r\n");
        final ResultSet rs = getResult(tableName, dbName);
        try {
            while (rs.next()) {
                String para = rs.getString(1);
                if("id".equals(para)) {
                    continue;
                }
                sb.append("\"<if test=\\\"");
                sb.append(paraName);
                sb.append(".");
                sb.append(para);
                sb.append("!=null\\\">");
                sb.append(para);
                sb.append("=#{");
                sb.append(paraName);
                sb.append(".");
                sb.append(para);
                sb.append("},</if>\" + ");
                sb.append("\r\n");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        sb.append("\"where id = #{");
        sb.append(paraName).append(".id}\"");
        sb.append("\r\n");
        sb.append("\"</script>\")");

        return sb.toString().replace("}, where", "} where").replace(",</if>\" + \r\n\"where", "</if>\" + \r\n\"where");
    }

    /*
    public void genSelect() {

        String paraName = GenUtil.replaceUnderLineWithOutFirstLetter(tableName);

        StringBuffer sb = new StringBuffer("select ");

        final ResultSet rs = getResult(tableName, dbName);
        try {
            while (rs.next()) {
                String para = rs.getString(1);
                sb.append(para);
                sb.append(", ");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        sb.append("from ");
        sb.append(tableName);
        sb.append("where xxx = #{");
        sb.append(paraName).append(".xxx}");

        System.out.println(sb.toString().replace(", from", "} from"));
    }*/

}
